//
//  HECardInfoViewController.m
//  huien
//
//  Created by yiban on 2017/7/6.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "HECardInfoViewController.h"
#import "iCardCell.h"
@interface HECardInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
}
@property(nonatomic,strong)LcbUITableView *lcbTableView;
@property(nonatomic,strong)NSMutableArray *menuArray;

@end

@implementation HECardInfoViewController
@synthesize lcbTableView=_lcbTableView;
@synthesize menuArray=_menuArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"银行卡信息";
    if (!_menuArray) {
        _menuArray =[[NSMutableArray alloc] init];
    }
    [self initTableView];
   
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     [self loadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    if (_lcbTableView) {
        [_lcbTableView removeFromSuperview];
        _lcbTableView=nil;
    }
    [LcbLog log:@"HESettingViewController--------->dealloc"];
}
#pragma mark - tableview delegate
-(void)initTableView
{
    //
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 60, 44);
    backButton.tag =100;
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:backButton];
    //
    if (_lcbTableView) {
        [_lcbTableView removeFromSuperview];
        _lcbTableView=nil;
    }
    _lcbTableView =[[LcbUITableView alloc] initWithFrame:CGRectMake(0, YB_HeaderHeight, SCREEN_WIDTH, SCREEN_HEIGHT-YB_HeaderHeight) style:UITableViewStylePlain];
    _lcbTableView.dataSource=self;
    _lcbTableView.delegate=self;
    _lcbTableView.backgroundColor=[UIColor clearColor];
    _lcbTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_lcbTableView];
}
-(void)clickNavEvent:(id)sender
{
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count == 1) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}
//
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.menuArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [iCardCell returnCellHeight];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    iCardCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"iCardCell"];
    if (cell == nil) {
        cell = [[iCardCell  alloc]initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"iCardCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row>=self.menuArray.count) {
        return cell;
    }
    ICarModel *infomation= [self.menuArray objectAtIndex:indexPath.row];
    [cell showInfo:infomation indexPath:indexPath];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(void)loadData
{
    SessionModel *sessionModel =[HEShare sessionModel];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:sessionModel.sessionId forKey:@"sessionId"];
    [dict setValue:[NSString stringWithFormat:@"%ld",sessionModel.session_user.userId] forKey:@"userId"];
    
    [HEApi bankListWithParam:dict serviceblock:^(id response, NSString *errorMsg) {
        if (response) {
            if ([response isKindOfClass:[LcbBaseModel class]]) {
                LcbBaseModel *baseModel = (LcbBaseModel*)response;
                [[NativeRouter sharedNativeRouter] codeHander:wSelf params:baseModel completion:nil];
            }else if ([response isKindOfClass:[NSArray class]]) {
                wSelf.menuArray = (NSMutableArray*)response;
                [wSelf.lcbTableView reloadData];
            }
        }
        
    }];
}
@end
